package com.zlx.sharelive.activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.BaseBean;
import com.zlx.module_base.base_api.res_data.DelUserAccountBody;
import com.zlx.module_base.base_api.res_data.UserAccountListBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_base.base_util.StringUt;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 银行卡管理
 */
public class BankCardListAc extends BaseAc {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private List<UserAccountListBean.InfoDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<UserAccountListBean.InfoDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_bank_card_list;
    }

    @Override
    public void initViews() {
        super.initViews();

        initRecyclerView();
        getUserAccountList();
    }

    @Override
    protected void onStart() {
        super.onStart();

        getUserAccountList();
    }

    private void getUserAccountList() {
        ApiUtil.getMoneyApi().getUserAccountList(SPUtil.getToken(this)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<UserAccountListBean>>() {
            @Override
            public void onSuccess(ApiResponse<UserAccountListBean> data) {
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            mList.addAll(data.getData().getInfo());
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }));
    }

    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecyclerViewAdapter<UserAccountListBean.InfoDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, UserAccountListBean.InfoDTO entity, int position) {

                holder.setText(R.id.tv_bank, entity.getBankBranch());

                if (!StringUt.isEmpty(entity.getAccountBank())) {
                    char[] code = entity.getAccountBank().toCharArray();
                    String bankCode = "";
                    for (int i = 0; i < code.length; i++) {
                        if (i < 4) {
                            bankCode += code[i];
                            if (i == 3) {
                                bankCode += "  ";
                            }
                        } else if (i > 3 && i < 8) {
                            bankCode += "*";
                            if (i == 7) {
                                bankCode += "  ";
                            }
                        } else if (i > 7 && i < 12) {
                            bankCode += "*";
                            if (i == 11) {
                                bankCode += "  ";
                            }
                        } else if (i > 11) {
                            bankCode += code[i];
                        }
                    }
                    holder.setText(R.id.tv_bank_code, bankCode);

                    ((TextView) holder.getView(R.id.tv_comm)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog(entity.getId());
                        }
                    });
                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_bank_card_list;
            }
        };

        rv_data.setAdapter(mAdapter);
    }

    private void dialog(String id) {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(BankCardListAc.this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(BankCardListAc.this, R.layout.dialog_cancel, null);
        dialog.setContentView(view);

        TextView tv_text = view.findViewById(R.id.tv_text);

        tv_text.setText("是否要解除该银行卡");

        TextView tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        TextView tv_determine = view.findViewById(R.id.tv_determine);
        tv_determine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                DelUserAccountBody delUserAccountBody = new DelUserAccountBody();
                delUserAccountBody.setAccountId(id);
                ApiUtil.getMoneyApi().delUserAccount(SPUtil.getToken(BankCardListAc.this),
                        id).observe(BankCardListAc.this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<BaseBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<BaseBean> data) {
                        dialog.dismiss();
                        if (null != data) {
                            if (null != data.getData()) {
                                if (data.getData().getCode() == 0) {
                                    getUserAccountList();
                                } else {
                                    Toast.makeText(BankCardListAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                }));
            }
        });

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //设置弹出动画
//        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

    }

    @OnClick({R.id.iv_back, R.id.tv_bank_card})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_bank_card:
                startActivity(new Intent(this, AddBankCardAc.class));
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
